using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using Microsoft.Practices.Prism.Regions;
using SMSManager.Commn.Interfaces;
using SMSManager.Commn.Interfaces.ViewModels;
using SMSManager.Commn.Interfaces.Views;
using SMSManager.WpfClient.Properties;

namespace SMSManager.WpfClient.ViewModels
{
	[Export(typeof (ISMSServiceListViewModel))]
	[PartCreationPolicy(CreationPolicy.NonShared)]
	public class SMSServiceListViewModel : ISMSServiceListViewModel
	{
		private readonly CompositionContainer _container;
		private readonly IRegionManager _regionManager;
		private readonly ISMSSettings _smsSettings;
		private IRegion _region;
		private ISMSServiceViewModel _selectedItem;

		[ImportingConstructor]
		public SMSServiceListViewModel(CompositionContainer container)
		{
			_container = container;

			_smsSettings = container.GetExportedValue<ISMSSettings>();
			SetItems(container, _smsSettings);

			_selectedItem = Items.FirstOrDefault(x => x.SMSServiceName == _smsSettings.SMSServiceName);

			//register the event handler to add the views to sms service region.
			_regionManager = container.GetExportedValue<IRegionManager>();
			_regionManager.Regions.CollectionChanged += RegionsCollectionChanged;
		}

		private void ActivateView()
		{
			if (_region != null && SelectedItem != null)
			{
				object view = _region.GetView(!SelectedItem.LoginViewModel.IsLogin
				                              	? Resources.LoginViewName
				                              	: Resources.SMSSendViewName);
				_region.Activate(view);
			}
		}

		private void SetItems(CompositionContainer container, ISMSSettings smsSettings)
		{
			Items = new List<ISMSServiceViewModel>();
			foreach (ISMSService smsService in container.GetExportedValues<ISMSService>())
			{
				//get ISMSServiceViewModel
				var smsServiceViewModel = container.GetExportedValue<ISMSServiceViewModel>();
				smsServiceViewModel.SMSService = smsService;
				IUser user = smsSettings.GetUser(smsService.Name);
				if (user == null)
				{
					user = container.GetExportedValue<IUser>();
					smsSettings.AddUser(smsService.Name, user);
				}
				smsServiceViewModel.User = user;
				smsServiceViewModel.SMSInfo = smsSettings.SMSInfo;
				Items.Add(smsServiceViewModel);
			}
		}

		private void RegionsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			if (e.Action == NotifyCollectionChangedAction.Add)
			{
				_region = (IRegion) e.NewItems[0];
				if (_region.Name == Resources.SMSServiceRegionName)
				{
					//add the views to the region.
					var loginView = _container.GetExportedValue<ILoginView>();
					_region.Add(loginView, Resources.LoginViewName);
					var smsSendView = _container.GetExportedValue<ISMSSendView>();
					_region.Add(smsSendView, Resources.SMSSendViewName);
					var smsResultView = _container.GetExportedValue<ISMSResultView>();
					_region.Add(smsResultView, Resources.SMSResultViewName);

					ActivateView();

					//remove the handler as 1 time is only executed.
					_regionManager.Regions.CollectionChanged -= RegionsCollectionChanged;
				}
			}
		}

		public ISMSServiceViewModel SelectedItem
		{
			get { return _selectedItem; }
			set
			{
				_selectedItem = value;
				_smsSettings.SMSServiceName = value.SMSServiceName;
				ActivateView();
			}
		}

		public IList<ISMSServiceViewModel> Items { get; private set; }
	}
}